package group.sicher.log.model;

import group.sicher.log.util.JsonUtil;
import group.sicher.log.util.TraceUtil;
import lombok.Getter;
import org.aspectj.lang.JoinPoint;

/**
 * @author Donald
 * @description
 * @date 2019-08-05
 **/
@Getter
public class MethodRecord {
    private String requestId;
    private String className;
    private String methodName;
    private String params;
    private String result;
    private long cost;
    private String exceptionMsg;

    public static MethodRecord of(JoinPoint joinPoint, Object result, long cost) {
        MethodRecord record = new MethodRecord();
        record.requestId = TraceUtil.getRequestId();
        record.className = joinPoint.getTarget().getClass().getName();
        record.methodName = joinPoint.getSignature().getName();
        record.params = JsonUtil.toJson(joinPoint.getArgs());
        record.result = JsonUtil.toJson(result);
        record.cost = cost;
        record.exceptionMsg = "";
        return record;
    }

    public static MethodRecord of(JoinPoint joinPoint, Throwable e) {
        MethodRecord record = new MethodRecord();
        record.requestId = TraceUtil.getRequestId();
        record.className = joinPoint.getTarget().getClass().getName();
        record.methodName = joinPoint.getSignature().getName();
        record.params = JsonUtil.toJson(joinPoint.getArgs());
        record.result = "";
        record.cost = 0;
        record.exceptionMsg = e.getMessage();
        return record;
    }
}
